/**
 * Created by John on 2016/5/19.
 */

var demo = new sample();
// demo.name;

// demo.log();

// demo.createServer();

// demo.readFile();

// demo.readFileSync();

// demo.readFile2();

// demo.useEvent();

// demo.getModule();

// demo.getModule2();

// demo.getHello();

// demo.getPackage();

// demo.debugCommand();

function sample() {
    this.name = "第3章";

    this.log = function () {
        console.log("Hello world");
        console.log("%s: %d", "Hello", 25);
    };

    this.createServer = function () {
        var http = require("http");
        http.createServer(function (req, res) {
            res.writeHead(200, {"Content-Type": "text/html"});
            res.write("<h1>Node.js</h1>");
            res.write("<p>Hello world</p>");
            res.write("<p>I'm form china</p>")
        }).listen(3000);
        console.log("HTTP server is listening at port 3000.");
    };

    this.readFile = function () {
        var fs = require("fs");
        fs.readFile("file.txt", "utf-8", function (err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(data);
                }
            }
        );
        console.log("end");
    };

    this.readFileSync = function () {
        var fs = require("fs");
        var data = fs.readFileSync("file.txt", "utf-8");
        console.log(data);
        console.log("end");
    };

    this.readFile2 = function () {
        function readFileCallBack(err, data) {
            if (err) {
                console.log(err)
            } else {
                console.log(data);
            }
        };

        var fs = require("fs");
        fs.readFile("file.txt", "utf-8", readFileCallBack);
        console.log("end");
    };

    this.useEvent = function () {
        var eventEmitter = require("events").EventEmitter;
        var event = new eventEmitter();
        event.on("some_event", function () {
            console.log("some_event occured.");
        });
        setTimeout(function () {
            event.emit("some_event");
        }, 1000);
    }

    this.getModule = function () {
        var myModule = require("./chapter03.module.js");
        myModule.setName("BYVoid");
        myModule.sayHello();
        return myModule;
    };

    this.getModule2 = function () {
        var myModule = require("./chapter03.module.js");
        myModule.setName("BYVoid");

        var myModule = require("./chapter03.module.js");
        myModule.setName("BYVoid 2");

        myModule.sayHello();
        return myModule;
    };

    this.getHello = function () {
        var hello = require("./chapter03.module.singleInstance.js");
        hello = new hello();
        hello.setName("BYVoid");
        hello.sayHello();
        return hello;
    };

    this.getPackage = function () {
        var package = require("./chapter03.package/lib/interface.js");
        package.hello();
        return package;
    }

    this.debugCommand = function () {
        var a = 1;
        var b = "world";
        var c = function (x) {
            console.log("hello " + x + a);
        };
        c(25);
    };

    this.process = function () {
        console.log(process.argv);

    }

    this.stdout = function () {
        process.stdin.resume();
        process.stdin.on("data", function (data) {
            process.stdout.write("read from console: " + data.toString());
        })
    }

    this.nextTick = function () {
        function doSomething(args, callback) {
            somethingComplicated(args);
            process.nextTick(callback);
        }

        function compute() {
            value = 999999999;
            while (value != 0) {
                value--;
            }
        }

        function somethingComplicated(args) {
            console.log(args);
        }

        doSomething(0, compute);
    }
}




